package swingUI;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.math.BigDecimal;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import com.trsjh.db.controller.Home;
import com.trsjh.db.domains.Good;
import com.trsjh.db.domains.GoodsType;

import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.JButton;

public class GoodUI extends JPanel implements MyUI{

//	private BindingGroup m_bindingGroup;
	private com.trsjh.db.domains.Good good = new com.trsjh.db.domains.Good();
	private JComboBox brandJComboBox;
	private JTextPane descriptionJTextPane;
	private JTextField nameJTextField;
	private JTextField photosJTextField;
	private Set<String> bands=new LinkedHashSet<String>();
	private Set<GoodsType> types=new LinkedHashSet<GoodsType>();
	private JSpinner lowPrice;
	private JSpinner price;
	private JComboBox typesBox;
	public GoodUI(com.trsjh.db.domains.Good newGood) {
		this();
		setGood(newGood);
	}

	public GoodUI() {
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[] { 0, 0, 0 };
		gridBagLayout.rowHeights = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
		gridBagLayout.columnWeights = new double[] { 0.0, 1.0, 1.0E-4 };
		gridBagLayout.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0E-4 };
		setLayout(gridBagLayout);

		JLabel brandLabel = new JLabel("品牌:");
		GridBagConstraints labelGbc_0 = new GridBagConstraints();
		labelGbc_0.insets = new Insets(5, 5, 5, 5);
		labelGbc_0.gridx = 0;
		labelGbc_0.gridy = 0;
		add(brandLabel, labelGbc_0);

		brandJComboBox = new JComboBox();
		brandJComboBox.setEditable(true);
		
		this.initBoxs();
		//DefaultComboBoxModel cm=(DefaultComboBoxModel) brandJComboBox.getModel();
		brandJComboBox.setModel(new DefaultComboBoxModel(){
			@Override
			public Object getElementAt(int index) {
				return bands.toArray()[index];
			}
			@Override
			public int getSize() {
				return bands.size();
			}
		});
		
		GridBagConstraints componentGbc_0 = new GridBagConstraints();
		componentGbc_0.insets = new Insets(5, 0, 5, 0);
		componentGbc_0.fill = GridBagConstraints.HORIZONTAL;
		componentGbc_0.gridx = 1;
		componentGbc_0.gridy = 0;
		add(brandJComboBox, componentGbc_0);

		JLabel descriptionLabel = new JLabel("描述:");
		GridBagConstraints labelGbc_1 = new GridBagConstraints();
		labelGbc_1.insets = new Insets(5, 5, 5, 5);
		labelGbc_1.gridx = 0;
		labelGbc_1.gridy = 1;
		add(descriptionLabel, labelGbc_1);

		descriptionJTextPane = new JTextPane();
		GridBagConstraints componentGbc_1 = new GridBagConstraints();
		componentGbc_1.insets = new Insets(5, 0, 5, 0);
		componentGbc_1.fill = GridBagConstraints.HORIZONTAL;
		componentGbc_1.gridx = 1;
		componentGbc_1.gridy = 1;
		add(descriptionJTextPane, componentGbc_1);

		JLabel nameLabel = new JLabel("名字:");
		GridBagConstraints labelGbc_2 = new GridBagConstraints();
		labelGbc_2.insets = new Insets(5, 5, 5, 5);
		labelGbc_2.gridx = 0;
		labelGbc_2.gridy = 2;
		add(nameLabel, labelGbc_2);

		nameJTextField = new JTextField();
		GridBagConstraints componentGbc_2 = new GridBagConstraints();
		componentGbc_2.insets = new Insets(5, 0, 5, 0);
		componentGbc_2.fill = GridBagConstraints.HORIZONTAL;
		componentGbc_2.gridx = 1;
		componentGbc_2.gridy = 2;
		add(nameJTextField, componentGbc_2);

		JLabel photosLabel = new JLabel("图片地址:");
		GridBagConstraints labelGbc_3 = new GridBagConstraints();
		labelGbc_3.insets = new Insets(5, 5, 5, 5);
		labelGbc_3.gridx = 0;
		labelGbc_3.gridy = 3;
		add(photosLabel, labelGbc_3);

		photosJTextField = new JTextField();
		GridBagConstraints componentGbc_3 = new GridBagConstraints();
		componentGbc_3.insets = new Insets(5, 0, 5, 0);
		componentGbc_3.fill = GridBagConstraints.HORIZONTAL;
		componentGbc_3.gridx = 1;
		componentGbc_3.gridy = 3;
		add(photosJTextField, componentGbc_3);
		
		JLabel label = new JLabel("底价");
		GridBagConstraints gbc_label = new GridBagConstraints();
		gbc_label.insets = new Insets(0, 0, 5, 5);
		gbc_label.gridx = 0;
		gbc_label.gridy = 4;
		add(label, gbc_label);
		
		lowPrice = new JSpinner();
		lowPrice.setModel(new SpinnerNumberModel(new Float(0), new Float(0), null, new Float(0.1)));
		lowPrice.setAutoscrolls(true);
		GridBagConstraints gbc_lowPrice = new GridBagConstraints();
		gbc_lowPrice.fill = GridBagConstraints.HORIZONTAL;
		gbc_lowPrice.insets = new Insets(0, 0, 5, 0);
		gbc_lowPrice.gridx = 1;
		gbc_lowPrice.gridy = 4;
		add(lowPrice, gbc_lowPrice);
		
		JLabel label_1 = new JLabel("售价");
		GridBagConstraints gbc_label_1 = new GridBagConstraints();
		gbc_label_1.insets = new Insets(0, 0, 5, 5);
		gbc_label_1.gridx = 0;
		gbc_label_1.gridy = 5;
		add(label_1, gbc_label_1);
		
		price = new JSpinner();
		price.setModel(new SpinnerNumberModel(new Float(0), new Float(0), null, new Float(0.1)));
		GridBagConstraints gbc_price = new GridBagConstraints();
		gbc_price.insets = new Insets(0, 0, 5, 0);
		gbc_price.fill = GridBagConstraints.HORIZONTAL;
		gbc_price.gridx = 1;
		gbc_price.gridy = 5;
		add(price, gbc_price);
		
		JLabel label_2 = new JLabel("商品类型");
		GridBagConstraints gbc_label_2 = new GridBagConstraints();
		gbc_label_2.anchor = GridBagConstraints.EAST;
		gbc_label_2.insets = new Insets(0, 0, 5, 5);
		gbc_label_2.gridx = 0;
		gbc_label_2.gridy = 6;
		add(label_2, gbc_label_2);
		
		typesBox = new JComboBox();
		GridBagConstraints gbc_typesBox = new GridBagConstraints();
		gbc_typesBox.insets = new Insets(0, 0, 5, 0);
		gbc_typesBox.fill = GridBagConstraints.HORIZONTAL;
		gbc_typesBox.gridx = 1;
		gbc_typesBox.gridy = 6;
		add(typesBox, gbc_typesBox);
		
		typesBox.setModel(new DefaultComboBoxModel(){
			@Override
			public Object getElementAt(int index) {
				return ((GoodsType)(types.toArray()[index]));
			}
			@Override
			public int getSize() {
				return types.size();
			}
			
		});
		JButton button = new JButton("保存");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Home home=new Home();
				Good good=new Good();
				if(brandJComboBox.getSelectedItem()!=null)
					good.setBrand(brandJComboBox.getSelectedItem().toString());
				good.setDescription(descriptionJTextPane.getText());
				good.setName(nameJTextField.getText());
				good.setPhotos(photosJTextField.getText());
				good.setLowerPrice(new BigDecimal(((Float)lowPrice.getValue())));
				good.setPrice(new BigDecimal((Float)price.getValue()));
				good.setGoodsType((GoodsType)typesBox.getSelectedItem());
				home.beginTransaction();
				home.persist(good);
				home.commit();
			}
		});
		GridBagConstraints gbc_button = new GridBagConstraints();
		gbc_button.gridx = 1;
		gbc_button.gridy = 7;
		add(button, gbc_button);

//		if (good != null) {
//			m_bindingGroup = initDataBindings();
//		}
	}

//	protected BindingGroup initDataBindings() {
//		BeanProperty<com.trsjh.db.domains.Good, java.lang.String> brandProperty = BeanProperty
//				.create("brand");
//		BeanProperty<javax.swing.JComboBox, java.lang.Integer> selectedIndexProperty = BeanProperty
//				.create("selectedIndex");
//		AutoBinding<com.trsjh.db.domains.Good, java.lang.String, javax.swing.JComboBox, java.lang.Integer> autoBinding = Bindings
//				.createAutoBinding(AutoBinding.UpdateStrategy.READ, good,
//						brandProperty, brandJComboBox, selectedIndexProperty);
//		autoBinding.bind();
//		//
//		BeanProperty<com.trsjh.db.domains.Good, java.lang.String> descriptionProperty = BeanProperty
//				.create("description");
//		BeanProperty<javax.swing.JTextPane, java.lang.String> textProperty = BeanProperty
//				.create("text");
//		AutoBinding<com.trsjh.db.domains.Good, java.lang.String, javax.swing.JTextPane, java.lang.String> autoBinding_1 = Bindings
//				.createAutoBinding(AutoBinding.UpdateStrategy.READ, good,
//						descriptionProperty, descriptionJTextPane, textProperty);
//		autoBinding_1.bind();
//		//
//		BeanProperty<com.trsjh.db.domains.Good, java.lang.String> nameProperty = BeanProperty
//				.create("name");
//		BeanProperty<javax.swing.JTextField, java.lang.String> textProperty_1 = BeanProperty
//				.create("text");
//		AutoBinding<com.trsjh.db.domains.Good, java.lang.String, javax.swing.JTextField, java.lang.String> autoBinding_2 = Bindings
//				.createAutoBinding(AutoBinding.UpdateStrategy.READ, good,
//						nameProperty, nameJTextField, textProperty_1);
//		autoBinding_2.bind();
//		//
//		BeanProperty<com.trsjh.db.domains.Good, java.lang.String> photosProperty = BeanProperty
//				.create("photos");
//		BeanProperty<javax.swing.JTextField, java.lang.String> textProperty_2 = BeanProperty
//				.create("text");
//		AutoBinding<com.trsjh.db.domains.Good, java.lang.String, javax.swing.JTextField, java.lang.String> autoBinding_3 = Bindings
//				.createAutoBinding(AutoBinding.UpdateStrategy.READ, good,
//						photosProperty, photosJTextField, textProperty_2);
//		autoBinding_3.bind();
//		//
//		BindingGroup bindingGroup = new BindingGroup();
//		bindingGroup.addBinding(autoBinding);
//		bindingGroup.addBinding(autoBinding_1);
//		bindingGroup.addBinding(autoBinding_2);
//		bindingGroup.addBinding(autoBinding_3);
//		//
//		return bindingGroup;
//	}

	public com.trsjh.db.domains.Good getGood() {
		return good;
	}

	public void setGood(com.trsjh.db.domains.Good newGood) {
		setGood(newGood, true);
	}

	public void setGood(com.trsjh.db.domains.Good newGood, boolean update) {
		good = newGood;
//		if (update) {
//			if (m_bindingGroup != null) {
//				m_bindingGroup.unbind();
//				m_bindingGroup = null;
//			}
//			if (good != null) {
//				m_bindingGroup = initDataBindings();
//			}
//		}
	}
	@SuppressWarnings("unchecked")
	public void initBoxs(){
		if(types.size()==0){
			Home home=new Home();
			types.addAll(home.list(Home.LIST_GOODTYPES,null));
		}
		if(bands.size()==0){
			Home home=new Home();
			bands.addAll(home.list(Home.LIST_GOOD_BANDS,null));
		}
	}

	@Override
	public void refresh() {
		// TODO Auto-generated method stub
		this.initBoxs();
	}
}
